package com.tm.szb.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.szb.commons.R;
import com.tm.szb.dto.SetmealDTO;
import com.tm.szb.service.SetmealService;
import com.tm.szb.vo.SetmealVO;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("setmeal")
public class SetmealController {



    private final SetmealService setMealService;

    public SetmealController(SetmealService setMealService) {
        this.setMealService = setMealService;
    }


    @PostMapping
    public R saveSetmeal(@RequestBody SetmealDTO setmealDTO){
        setMealService.saveSetmeal(setmealDTO);
        return R.success();
    }


    @GetMapping("page")
    public R page(@RequestParam(required = false,defaultValue = "1") Integer page,
                  @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                  @RequestParam(required = false) String name){

        Page<SetmealVO> pageInfo = setMealService.pageList(page,pageSize,name);
        return R.success(pageInfo);

    }


    @GetMapping("/{id}")
    public R getSetMeal(@PathVariable Long id){
        SetmealVO setmealVO = setMealService.getSetMeal(id);
        return R.success(setmealVO);
    }

    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        setMealService.updateSetmeal(setmealDTO);
        return R.success();
    }


    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,String ids){
        List<Long> idList = Arrays.stream(ids.split(",")).map(idStr -> {
            return Long.parseLong(idStr);
        }).collect(Collectors.toList());
        setMealService.updateStatus(status,idList);
        return R.success();
    }


    @DeleteMapping
    public R deleteSetmeal(String ids){
        List<Long> longList = Arrays.stream(ids.split(",")).map(idStr -> {
            return Long.parseLong(idStr);
        }).collect(Collectors.toList());
        setMealService.deleteSetmeal(longList);
        return R.success();
    }

}
